<?php 
require_once 'Logger/Log.php';

class Logger
{
	const DEBUG     =       1;
	const INFO      =       2;
	const WARN	=	4;
	const ERROR     =	8;
	const FATAL     =	16;
	const ALL       =	32;
	protected static $_levelName    =    array(
	self::DEBUG  => 'DEBUG',
	self::INFO   => 'INFO',
	self::WARN   => 'WARN',
	self::ERROR  => 'ERROR',
	self::FATAL  => 'FATAL',
	self::ALL    => 'ALL',
	);
	protected static    $_configure   ;
    private static      $_logInstance  ;
    public static function levelName($level)
    {
        if( isset(self::$_levelName[$level]) )
            return self::$_levelName[$level];
         else
            return null;
    }
	/*public static function getLogger()
	{
		
	}*/
    
    
    /**
     * @return Logger_Log;
     */
	public static function getLogger($logName)
	{
		
		$log  =   new Logger_Log($logName);
	
		if(!isset(self::$_configure[$logName]))
		{
		     $log->offLog();
		     return $log;
		}
		 
		$log->setLevle(self::$_configure[$logName]['level']);
		
		
		
		foreach(self::$_configure[$logName]['appenders'] as $appenderName)
		{
		    if( isset(self::$_configure['appenders'][$appenderName]) )
		    {
		        
		        $appenderConf =   self::$_configure['appenders'][$appenderName];
		       
		        $appender =   self::singletonAppender($appenderConf['class'],$appenderName,$appenderConf['options']);
		        $layuout  =    self::factoryLayout($appenderConf['layoutClass'],$appenderConf['layoutOptions']) ;
		        
		        $appender->setLayout($layuout);
		        $log->addAppender($appender);
		    }
		        
		        
		}
		
		return $log;
	}
	public static function initConfig($config = 'default')
	{
	    
	 /*   if(self::$_configure)
	       return ;*/
	    $file      =   "Logger/config/{$config}.php";
	    
	    self::$_configure       =   include($file);
	    
	}
	/**
	 * @return Logger_Appender_Abstraction
	 */
	public static function singletonAppender( $appenderClass,$name,array $options = array())
	{
	    static $instance;
	    
	    if(!isset($instance[$name]))
	    {
	         $instance[$name]   =   self::factoryAppender($name,$appenderClass,$options);
	         
	    }
	      
	      
	     return $instance[$name];
	}
	public static function factoryAppender($name, $class,$options)
	{
	    $file = str_replace('_',DIRECTORY_SEPARATOR,$class).'.php';
	    
	    
	    if(!class_exists($class))
	    	require_once $file;
		
		$app   =   new $class($name);
	    
	    $app->setOptions($options);
	    
	    return $app;
	}
	
	public static function factoryLayout( $class,$options)
	{
	    $file = str_replace('_',DIRECTORY_SEPARATOR,$class).'.php';
	    if(!class_exists($class))
	    	require_once $file;
		$object    =   new $class();
	    $object->setOptions($options);
	    return $object;
	}
	

}
Logger::initConfig();
